avatar Bite 353. Transform a Script Into a Command Line Interface (CLI)

There are several Python frameworks to create CLIs, check out this overview of CLI framework options

In this Bite we will leverage Typer to create a CLI based on a simple Python script.

Therefore, a script with a simple function will be provided so that you will only need to extend the provided script with Typer annotations to allow it to be called with typer.run(main).

Typing python script.py --help should return the following:

Usage: script.py [OPTIONS] a b

  CLI that allows you to add two numbers

Arguments:
a  The value of the first summand  [required]
b  The value of the second summand  [required]

Options:
--install-completion  Install completion for the current shell.
--show-completion     Show completion for the current shell, to copy it or
                      customize the installation.
  --help                Show this message and exit.


The Options are standard Typer switches which are available out-of-the-box, so you don't need to worry about them.

If this description is unclear, check out the tests to better understand what is required.

One last hint: Make sure to always use lowercase arguments, since there is a bug in click concerning mixed and upper case arguments.

Enjoy!

PS: Just use typer.Argument for now. If you stumble over typer.Option and wonder what the difference is, wait for the next Bite. :)

Login and get coding
go back Beginner level
Bitecoin 2X

35 out of 36 users completed this Bite.
Will you be the 36th person to crack this Bite?
Resolution time: ~57 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 5.0 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help